<?php

namespace app\store\service;
use app\common\model\User;
use app\Response;
use think\response\Json;

class LoginService
{
    private static $login_valid = 2592000;//token周期一个月

    private static $subsist_valid = 259200;//活跃用户存续时间

    /** 账号登录
     *
    */
    public static function loginService($data){
        $user = User::userInfo(['phone'=>$data['username']]);
        if($user){
            if(!in_array($user['type'],[24])){
                return  Response::wrapError(0,"该账号非门店账号!");
            }
            if($user['password'] !== User::encPassword($data['password'],$user['encryption'])){
                return  Response::wrapError(0,"账号或密码错误");
            }
            $token = self::setMyToken($user["uid"]);
            $res = ["token"=>$token,"type"=>$user["type"]];
            return Response::wrapData($res);
            //setMyToken
        }else{
            return Response::wrapError(0,"账号不存在!");
        }
    }

    /** 修改密码
     * @param $user_id int 用户ID
     * @param $passwordOld string 旧密码
     * @param $password string 新密码
     * @return Json
    */
    public static function userEditPasswordService($user_id,$passwordOld,$password){
        $userInfo = User::userInfo(["uid"=>$user_id]);
        if(empty($userInfo)){
            return Response::wrapError(-200,'非法请求');
        }
        if(!in_array($userInfo['type'],[22,23])){
            return Response::wrapError(0,"该账号非买家用户!");
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError(-200,"此账户已被禁用,如有疑问请联系平台客服!");
        }
        if(!empty($passwordOld)){
            if(User::encPassword($passwordOld,$userInfo['encryption'])!==$userInfo["password"]){
                return Response::wrapError(0,"旧密码输入错误!");
            }
        }
        if(User::encPassword($password,$userInfo['encryption'])==$userInfo["password"]){
            return Response::wrapData();
        }
        $res = User::forgetPassword($userInfo,$password);
        if($res){
            return Response::wrapData();
        }else{
            return Response::wrapError("密码修改失败,请稍后重试");
        }
    }


    /**
     * 校验用户token
     * @param int $user_id
     * @param array $data
     * @return string
     */
    public static function getUserIdFromToken($mute = false)
    {
        $token = app('request')->header('auth-token');
        if(empty($token)){
            return Response::wrapError(-200,"未识别到登录状态!");
        }
        if (app('cache')->has($token)) {
            $user_id = app('cache')->get($token);
            return $user_id;
        }else{
            return Response::wrapError(-200,"未获取到用户信息!");
        }
        if (!$mute||empty($user_id)){
            return Response::wrapError(-200,"请先进行登录!");
        }else{
            return 0;
        }
    }

    /**
     * 生成随机字符串
     * @param int $user_id
     * @param array $data
     * @return string
     */
    public static function setMyToken($user_id)
    {
        $token1 = self::str_random(9);
        $token2 = self::str_random(9);
        $token = $token1.$token2;
        if (app('cache')->has($token)) {
            self::setMyToken($user_id);
        }
        app('cache')->set($token, $user_id, self::$login_valid);
        User::userEdit(["uid"=>$user_id],["token"=>$token]);
        return $token;
    }

    /**
     * 生成随机字符串
     * @param int $user_id
     * @param array $data
     * @return string
     */
    public static function str_random($length = 16)
    {
        $string = '';
        while (($len = strlen($string)) < $length) {
            $size = $length - $len;
            $bytes = random_bytes($size);
            $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
        }
        return $string;
    }
}